map
Every other container finds an item by where it is: the third slot, the front of the line, the top of the pile. A map finds an item by a key - a name you choose for it. You store a value under a key, and later you give the key and get the value back. Like a phone book: you look up a name, and you do not care which page it is on.
A map holds pairs. Each pair is a key and the value kept under it. In map<string,int>, the keys are pieces of text and the values are whole numbers - a score for each name. In map<int,string> the keys are numbers and the values are text - a name for each id.
Reach for a map when you look things up by name or id instead of by position:
map<ref<obj_enemy>,int>, can keep one number per enemy./// @type {map<string,int>}
/// @capacity 16
var scores = {};/// @type names the key type and then the value type. /// @capacity is the most pairs the map can hold, and leaving it off gives you 8. Like every container on the GBA, its space is set aside when the game is built, not grown while it runs.
The same map with no comment: var scores = map_create(16, "", 0); (an empty map with text keys and whole-number values that holds 16 pairs) or var scores: map<string, int, 16> = {};. See map_create. All three are the same map, and a map given no size by any of them holds 8.
A map starts empty, and it is written with a pair of curly braces: {}. It is the only container that starts that way. Square brackets ([]) belong to the other containers, and a map refuses them. There is no way to start a map with pairs already in it; add them afterwards with map_set.
| Function | What it does | It gives back |
|---|---|---|
map_set(map, key, value) | stores value under key | true if it is stored, false if the key was new and the map was full |
map_get(map, key) | reads the value under key | the value |
map_has(map, key) | asks whether key is in the map | true or false |
map_remove(map, key) | takes out key and its value | true if it was there, false if not |
map_length(map) | counts the pairs | the count |
map_clear(map) | empties it | nothing |
map_copy(map) | makes a second map just like it, with the same room | the new map |
Each of map_set, map_get, map_has and map_remove takes about the same short time whether the map holds five pairs or five hundred. That speed is the reason to use a map instead of searching a list: a list has to check its items one after another.
map_set on a new key adds a pair. map_set on a key that is already in the map replaces its value. It does not add a second pair. So map_length counts each key once.
Because replacing never makes the map bigger, it always works, even when the map is full. Only a brand-new key can be turned away, and then map_set returns false and changes nothing.
map_get on a key that was never set does not crash and does not add anything. It hands back a plain default value: 0 for a whole number, 0.0 for a fractional one, false, an empty piece of text, or nothing for an instance reference.
That default is an ordinary value, and 0 could be a real score. map_get(scores, "bob") giving 0 does not tell you whether bob's score is zero or bob was never entered. When the difference matters, ask first with map_has.
/// @type {map<string,int>}
/// @capacity 16
var scores = {};
map_set(scores, "alice", 10);
map_set(scores, "alice", 25); // replaces 10 - still one pair
var alice = map_get(scores, "alice"); // 25
var count = map_length(scores); // 1
var has_bob = map_has(scores, "bob"); // false
var bob = map_get(scores, "bob"); // 0 - the default, not a stored scoreA map's capacity must be a power of two: 2, 4, 8, 16, 32, and so on. This comes from the Game Boy Advance module's container the map is built on, which stores its pairs in a hash table and refuses any other size. The default of 8 already follows the rule. Write /// @capacity 10 and the project is refused before the game runs, with a message naming the next size up (16). The queue and the set have the same rule.
A key has to be something the map can turn into a lookup number, called hashing. These work as keys: int, real, bool, string, color, an instance reference (ref<obj_enemy>), and any of your project's asset ids (a sprite, sound, song, font, tileset, room, path, effect or background). A struct cannot be a key, and the project is refused where the map is declared. Two strings are the same key when they hold the same text. Two instance references are the same key when they point at the same instance.
A value can be anything a variable can hold, with one limit for structs, below. The editor stops a map of structs where you declare it.
A map answers "is this key here, and what is under it" very quickly. It does not offer a way to go through every key. There is no loop that visits each pair, and the order the pairs are stored in depends on how the keys are turned into lookup numbers, not on the order you added them.
When a game needs to go through every pair, keep a list of the keys alongside the map, add to both together, and walk the list:
/// @type {map<string,int>}
/// @capacity 16
var scores = {};
/// @type {list<string>}
/// @capacity 16
var names = [];
if (not map_has(scores, "alice")) {
list_add(names, "alice"); // a new key - remember it in the list too
}
map_set(scores, "alice", 10);
for (var i = 0; i < list_length(names); i++) {
var who = names[i];
var points = map_get(scores, who);
}This costs one more container and one extra line each time you add a key, and gives you a walk in a fixed order.
[key] eitherA map cannot be read or written with square brackets. scores["alice"] is refused when the project is checked, with a message that a map cannot be indexed. map_get and map_set replace it safely: map_get gives a default for a missing key instead of crashing, and map_set returns false instead of overflowing a full map. Accessors explains the reasoning.
A map's value may be a number, text, a color, an asset id or an instance reference. It cannot be a struct yet. GBML learns what a struct looks like from a real value written in your code, and a map always starts empty ({}), so it has nothing to learn the shape from. If you write map<int, struct Cell>, the editor stops you at that line with "A map of structs cannot be built yet", before the game is built. This is a known gap with a tag and {}. There is a way round it: make the map with map_create and give a struct as the value example. var stats = map_create(8, "", { hp: 0, speed: 0 }); is an empty map whose values are structs with those two fields. The example is never stored; it only shows GBML the fields. Or keep the pieces in separate maps that share the same keys, or keep a reference to an instance and read the fields off that instead:
/// @type {map<int, ref<obj_enemy>>}
var enemy_of_id = {};The Structs in data containers page has the general rules.
Reach for a map when the question is "what goes with this name or id." If you only need "is it in here," and there is no value to keep, use a set. If position matters, use a list. Data containers compares all seven side by side.